var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
var httpFile = 'http://file.waijiaojun.com/';
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
var weike_id, littleClassCoverFlag, weike_content_id,imgArr=[];
Page({
  data: {
    index: 1,
    width: 120,
    transFlag: false,
    audioFlag: true,
    imgUrl: 'http://file.waijiaojun.com/waijiaojun/icon',
    littleClassCoverFlag: 'false',
  },
  onLoad: function (e) {
    weike_id = e.id;
    littleClassCoverFlag = e.coverFlag;
    this.setData({
      littleClassCoverFlag: littleClassCoverFlag,
    })
    this.littleClassData();
  },
  onReady: function (e) {

  },
  //课程内容数据请求
  littleClassData: function () {
    if (app.globalData.token !== null) {
      var that = this;
      wx.request({
        url: httpWeike + '/v4.2/student/token/find/content',
        data: {
          weike_id: weike_id,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          'token': app.globalData.token,
        },
        method: 'POST',
        success: function (res) {
          console.log('课程内容=', res)
          var imgData = res.data.obj.images;
          console.log(imgData);
          var partsData = res.data.obj.parts;
          for (var i = 0; i < imgData.length; i++) {
            imgData[i].url = 'http://file.waijiaojun.com/' + imgData[i].url
            imgArr.push(imgData[i].url);
          }
          for (var j = 0; j < partsData.length; j++) {
            var contentsData = partsData[j].contents;
            for (var k = 0; k < contentsData.length; k++) {
              if (contentsData[k].type == 'audio') {
                var TimeData = contentsData[k].durationFromat.split("′");
                if (TimeData.length == 2) {
                  var min = TimeData[0] - 0;
                  if (min >= 10) {
                    contentsData[k].width = 560;
                  } else {
                    contentsData[k].width = 340;
                  }
                }
                if (TimeData.length == 1) {
                  contentsData[k].width = 120;
                }
              }
            };
          };
          that.setData({
            littleClassData: res.data.obj,
            teacher_avatar_url: res.data.obj.teacher_avatar_url,
            partsData: partsData,
            // partsUnit: partsData[0],
            price: res.data.obj.price,
            studentScore: res.data.obj.studentScore
          })
        },
      })
    }
  },
  swiperChange: function (e) {
    // console.log(e.detail.current);
    var index = e.detail.current + 1
    this.setData({
      index: index,
    })
  },
  //预览PPT
  previewImageFun:function(e){
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url, 
      urls: imgArr 
    })
  },
  //播放音频资源
  audioPlayFun: function (e) {
    console.log('开始播放')
    console.log('点击的语音id' + e.currentTarget.dataset.weike_content_id);
    var audio_url = e.currentTarget.dataset.audio_url;
    var idx = e.currentTarget.dataset.idx;
    weike_content_id = e.currentTarget.dataset.weike_content_id;
    var sta = e.currentTarget.dataset.sta;
    var cover = e.currentTarget.dataset.cover;
    if (audio_url.slice(0, 1) !== '/') { audio_url = 'http://file.waijiaojun.com/' + audio_url }
    console.log('播放地址' + audio_url);
    var that = this;
    that.setData({
      audioFlag: false,
      weike_content_id: weike_content_id,
      // sta: true,
    })
    // this.littleClassData();
    //将播放数据传向后台,记录是否播放过
    if (cover == true) {
      wx.request({
        url: httpWeike + '/v4.3/student/token/content/audio/play/status/receive',
        data: {
          weike_content_id: weike_content_id,
          weike_id: weike_id,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          'token': app.globalData.token,
        },
        method: 'POST',
        success: function (res) {
        }
      })
    }
    if (cover == true) {
      wx.playBackgroundAudio({
        title: '',
        coverImgUrl: '',
        dataUrl: audio_url,
        success: function (res) {
          console.log(res);
          that.littleClassData();
          that.setData({           
          })
        },
        fail: function (res) {
          console.log('播放失败' + res)
        },
        complete: function (res) {
          console.log(res)
        },
      });
      wx.onBackgroundAudioStop(function () {
        that.setData({ audioFlag: true, })
      })
    }

  },
  audioPauseFun: function (e) {
    var weike_content_id2 = e.currentTarget.dataset.weike_content_id;
    if (weike_content_id2 == weike_content_id) {
      this.setData({
        audioFlag: true,
      })
      wx.pauseBackgroundAudio();
    }
    if (weike_content_id2 !== weike_content_id){
      this.audioPlayFun(e);
    }

  },
  onUnload: function () {
    wx.stopBackgroundAudio()
  },
  onHide: function () {
    wx.stopBackgroundAudio()
  },
  translateFun: function (e) {
    // var that = this;
    // console.log(this);
    // console.log(e.currentTarget);
    // console.log(e.currentTarget.dataset);
    // var datasetData = e.currentTarget.dataset
    // wx.request({
    //   url: 'http://fanyi.youdao.com/openapi.do?keyfrom=nativetalk&key=1189830333&type=data&doctype=json&version=1.1&q=' + datasetData.trans,
    //   method: 'GET',
    //   success: function (res) {
    //     e.currentTarget.dataset.transed=res.data.translation[0];
    //     console.log(e.currentTarget.dataset);
    //     that.setData({
    //       translation: e.currentTarget.dataset.transed,
    //       transFlag: true,
    //     });
    //   },
    //   fail: function (res) { },
    //   complete: function (res) { },
    // })
  }
})